package com.gz.steak.modules.system.service.impl;

import com.gz.steak.common.consts.Role;
import com.gz.steak.modules.system.mapper.SysMenuMapper;
import com.gz.steak.modules.system.mapper.SysRoleMenuMapper;
import com.gz.steak.modules.system.pojo.dataobject.SysMenu;
import com.gz.steak.modules.system.pojo.model.SysMenuRoleModel;
import com.gz.steak.modules.system.service.SysMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 刘牌
 * @Date 2020/3/6 0006 23:31
 * @Version 1.0
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {
    private final static Logger logger = LoggerFactory.getLogger(SysMenuServiceImpl.class);
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Override
    public List<SysMenu> findSideTree(String user, int menuType) {
        List<SysMenu> sysMenus = new ArrayList<>();
        List<SysMenu> menus = findByUserName(user);
        for (SysMenu menu : menus) {
            if (menu.getMenuParentId() == null || menu.getMenuParentId() == 0) {
                menu.setLevel(0);
                if(!exists(sysMenus, menu)) {
                    sysMenus.add(menu);
                }
            }
        }
        sysMenus.sort((o1, o2) -> o1.getOrderNum().compareTo(o2.getOrderNum()));
        findChildren(sysMenus, menus, menuType);
        return sysMenus;

    }

    public List<SysMenu> findByUserName(String user){
        if (user.equals(Role.ADMIN)){
            return sysMenuMapper.findAllMenu();
        }
        return sysRoleMenuMapper.findByUserName(user);
    }

    @Override
    public void save(SysMenu sysMenu) {
        if (sysMenu.getMenuParentId() == null){
            sysMenu.setMenuParentId(0L);
        }
        int i = sysMenuMapper.insertSelective(sysMenu);
    }

    @Override
    public List<Integer> findRoleHasMenuById(int id) {
        return sysRoleMenuMapper.findRoleHasMenuById(id);
    }

    @Override
    public void saveRoleHasMenu(SysMenuRoleModel sysMenuRoleModel) {
        for (int i = 0 ; i < sysMenuRoleModel.getRoleHasMenuIdArray().length ; i++){
            int i1 = sysRoleMenuMapper.insertIfRecordIsNotFound(sysMenuRoleModel.getId(), sysMenuRoleModel.getRoleHasMenuIdArray()[i]);
        }
    }

    private boolean exists(List<SysMenu> sysMenus, SysMenu sysMenu) {
        boolean exist = false;
        for(SysMenu menu:sysMenus) {
            if(menu.getId().equals(sysMenu.getId())) {
                exist = true;
            }
        }
        return exist;
    }


    private void findChildren(List<SysMenu> SysMenus, List<SysMenu> menus, int menuType) {
        for (SysMenu SysMenu : SysMenus) {
            List<SysMenu> children = new ArrayList<>();
            for (SysMenu menu : menus) {
                if(menuType == 1 && menu.getMenuType() == 2) {
                    // 如果是获取类型不需要按钮，且菜单类型是按钮的，直接过滤掉
                    continue ;
                }
                if (SysMenu.getId() != null && SysMenu.getId().equals(menu.getMenuParentId())) {
                    menu.setParentName(SysMenu.getMenuName());
                    menu.setLevel(SysMenu.getLevel() + 1);
                    if(!exists(children, menu)) {
                        children.add(menu);
                    }
                }
            }
            SysMenu.setChildren(children);
            children.sort((o1, o2) -> o1.getOrderNum().compareTo(o2.getOrderNum()));
            findChildren(children, menus, menuType);
        }
    }


}
